import request from '@/utils/request';
import { getRequest, putRequest, postRequest } from '../../utils/request';

// 查询用户列表
export function listUser(query) {
  return request({
    url: '/system/user/page',
    method: 'get',
    params: query
  });
}

// 获取用户精简信息列表
export function listSimpleUsers() {
  return request({
    url: '/system/user/list-all-simple',
    method: 'get'
  });
}

function praseStrEmpty(str) {
  if (!str || str === 'undefined' || str === 'null') {
    return '';
  }
  return str;
}
// 查询用户详细
export function getUser(userId) {
  return request({
    url: '/system/user/get?id=' + praseStrEmpty(userId),
    method: 'get'
  });
}

// 新增用户
export function addUser(data) {
  return request({
    url: '/system/user/create',
    method: 'post',
    data: data
  });
}

// 修改用户
export function updateUser(data) {
  return request({
    url: '/system/user/update',
    method: 'put',
    data: data
  });
}

// 删除用户
export function delUser(userId) {
  return request({
    url: '/system/user/delete?id=' + userId,
    method: 'delete'
  });
}

// 导出用户
export function exportUser(query) {
  return request({
    url: '/system/user/export',
    method: 'get',
    params: query,
    responseType: 'blob'
  });
}

// 用户密码重置
export function resetUserPwd(id, password) {
  const data = {
    id,
    password
  };
  return request({
    url: '/system/user/update-password',
    method: 'put',
    data: data
  });
}

// 用户状态修改
export function changeUserStatus(id, status) {
  const data = {
    id,
    status
  };
  return request({
    url: '/system/user/update-status',
    method: 'put',
    data: data
  });
}

// 查询用户个人信息
export function getUserProfile() {
  return request({
    url: '/system/user/profile/get',
    method: 'get'
  });
}

// 修改用户个人信息
export function updateUserProfile(data) {
  return request({
    url: '/system/user/profile/update',
    method: 'put',
    data: data
  });
}

// 用户密码重置
export function updateUserPwd(oldPassword, newPassword) {
  const data = {
    oldPassword,
    newPassword
  };
  return request({
    url: '/system/user/profile/update-password',
    method: 'put',
    data: data
  });
}

// 用户头像上传
export function uploadAvatar(data) {
  return request({
    url: '/system/user/profile/update-avatar',
    method: 'put',
    data: data
  });
}

// 下载用户导入模板
export function importTemplate() {
  return request({
    url: '/system/user/get-import-template',
    method: 'get',
    responseType: 'blob'
  });
}

// 授权用户区域
export const systemUserAssignWorkshop = (params) => putRequest('/system/user/assign-workshop', params);
// 获取用户区域
export const systemUserListUserWorkshop = (params) => getRequest('/system/user/list-user-workshop', params);
// 授权用户工序
export const systemUserAssignProcess = (params) => putRequest('/system/user/assign-process', params);

// 授权用户班组数据
export const systemUserAssignGroup = (params) => putRequest('/system/user/assign-group', params);
// 获取用户班组数据
export const systemUserListUserGroup = (params) => getRequest('/system/user/list-user-group', params);
// 获取用户工序数据
export const systemUserListUserProcess = (params) => getRequest('/system/user/list-user-process', params);

export const spinProcessGetByWorkshopIds = (params) => postRequest('/spin/process/get-by-workshopIds', params);
export const personnelGroupGetByWorkshopIds = (params) => postRequest('/personnel/group/get-by-workshopIds', params);
